package com.cn.serverline.lineicp.productionLine.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cn.serverline.lineicp.productionLine.constants.ProductionLineConstant;
import com.cn.serverline.lineicp.productionLine.entity.WorkSection;
import com.cn.serverline.lineicp.productionLine.enums.WorkSectionException;
import com.cn.serverline.lineicp.productionLine.mapper.WorkSectionMapper;
import com.cn.serverline.lineicp.productionLine.param.WorkSectionEditParam;
import com.cn.serverline.lineicp.productionLine.param.WorkSectionAddParam;
import com.cn.serverline.lineicp.productionLine.param.WorkSectionIdParam;
import com.cn.serverline.lineicp.productionLine.service.IWorkSectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.serverline.lineicp.productionLine.util.DictUtil;
import org.springframework.stereotype.Service;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;

import javax.annotation.Resource;


/**
 * <p>
 * 滚动计划-工段表 服务实现类
 * </p>
 *
 * @author Gmr
 * @since 2024-07-28
 */
@Service("WorkSectionServiceImpl")
public class WorkSectionServiceImpl extends ServiceImpl<WorkSectionMapper, WorkSection> implements IWorkSectionService {

    // 字典服务
    @Resource(name = "dictUtil")
    private DictUtil dictUtil;


    /**
     * 新增
     *
     * @param workSectionAddParam 请求参数
     */
    @Override
    public void add(WorkSectionAddParam workSectionAddParam) {
        WorkSection workSection = BeanUtil.copyProperties(workSectionAddParam, WorkSection.class);
        // 校验参数
        checkParam(workSection);
        this.save(workSection);
    }

    /**
     * 删除
     *
     * @param workSectionIdParam 请求参数
     */
    @Override
    public void del(WorkSectionIdParam workSectionIdParam) {
        WorkSection workSection = getWorkSection(workSectionIdParam.getId());
        this.removeById(workSection);
    }

    /**
     * 编辑
     *
     * @param workSectionEditParam 请求参数
     */
    @Override
    public void edit(WorkSectionEditParam workSectionEditParam) {
        WorkSection workSection = getWorkSection(workSectionEditParam.getId());
        BeanUtil.copyProperties(workSectionEditParam, workSection);
        // 校验参数
        checkParam(workSection);
        this.updateById(workSection);
    }

    /**
     * 查询详情
     *
     * @param workSectionIdParam 请求参数
     */
    @Override
    public WorkSection detail(WorkSectionIdParam workSectionIdParam) {
        return getWorkSection(workSectionIdParam.getId());
    }

    /**
     * 分页查询
     *
     * @param workSection 请求参数
     */
    @Override
    public PageResult<WorkSection> findPage(WorkSection workSection) {
        LambdaQueryWrapper<WorkSection> wrapper = Wrappers.lambdaQuery();
        // 筛选主要属性
        /* wrapper.select(WorkSection::getId, WorkSection::getPositionName, WorkSection::getCode, WorkSection::getRemark,
        WorkSection::getPositionSort, BaseEntity::getCreateTime);*/

        Page<WorkSection> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    /**
     * 校验参数
     *
     * @param workSection 工段
     */
    private void checkParam(WorkSection workSection) {
        // 判断工段名是否重复
        if(obtainQuantity(workSection) > 0){
            throw new ServiceException(WorkSectionException.WORK_SECTION_DUPLICATE_NAME);
        }

        // 判断工段类型是否存在
        String workSectionCode = dictUtil.getCodeByTypeAndName(ProductionLineConstant.DICTIONARY_OF_SECTION_TYPES,workSection.getWorkSectionName());
        if(workSectionCode == null){
            throw new ServiceException(WorkSectionException.WORK_SECTION_TYPE_NOT_EXIST);
        }
        workSection.setSectionCode(workSectionCode);
    }

    /**
     * 获取工段
     *
     * @param id 工段ID
     */
    private WorkSection getWorkSection(Long id) {

        if(ObjectUtil.isEmpty(id)){
            throw new ServiceException(WorkSectionException.WORK_SECTION_ID_NOT_NULL);
        }

        WorkSection section = this.getById(id);
        if(ObjectUtil.isEmpty(section)){
            throw new ServiceException(WorkSectionException.WORK_SECTION_NOT_EXIST);
        }

        return section;
    }

    /**
     * 获取工段数量
     *
     * @param workSection 工段
     */
    private Long obtainQuantity(WorkSection workSection){
        LambdaQueryWrapper<WorkSection> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(workSection.getWorkSectionName()),
                        WorkSection::getWorkSectionName,
                        workSection.getWorkSectionName())
                .ne(ObjectUtil.isNotEmpty(workSection.getId()),
                        WorkSection::getId,
                        workSection.getId());
        return this.count(wrapper);
    }
}
